/**
 * @(#) TeamVacationProxy.as
 *
 * @date		2009. 02. 12.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	부서원 휴가  Proxy - Proxy
 * 				부서원들의 휴가 등록 사항을 조회한다.
 */
package com.skt.common_hr.work_sac.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.work_sac.WorkFacade;
	import com.skt.common_hr.work_sac.model.vo.TeamVacationVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class TeamVacationProxy extends CommonProxy
	{
		public static const NAME:String = "TeamVacationProxy";
		
		public var teamVacationList:ArrayCollection;
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function TeamVacationProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(pernr:String, month:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"] 		= pernr;
			param["I_BEGDA"]	= DateUtil.toBasicString(firstDay(month));
			param["I_ENDDA"]	= DateUtil.toBasicString(lastDay(month));
			loadData("Z_HR_0018", param, result, fault); 
		}
		
		private function firstDay(month:String):Date
		{
			var baseDate:Date = new Date();
			baseDate.fullYear = parseInt(month.substr(0,4));
			baseDate.month = parseInt(month.substr(4, 2)) -1;
			baseDate.date = 1;
			return DateUtil.startOfWeek(baseDate);
		}
		
		
		private function lastDay(month:String):Date
		{
			var baseDate:Date = new Date();
			baseDate.fullYear = parseInt(month.substr(0,4));
			baseDate.month = parseInt(month.substr(4, 2))-1;
			baseDate.month ++;
			baseDate.date = 1;
			baseDate.date --;
			return DateUtil.endOfWeek(baseDate);
		}




		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(retCode==0) {  
				teamVacationList = parseVacationList(event.result.RTAB);
				facade.sendNotification(WorkFacade.LOAD_TEAMVACATION_COMPLETE, this);
			}
			else {
				trace( "failed to load teamVacation list: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(WorkFacade.LOAD_TEAMVACATION_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			trace("Fault message in DateProxy: " + event.fault.faultString);
			facade.sendNotification(WorkFacade.LOAD_TEAMVACATION_FAILED, this);
		}
		
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 제증명서 종류 Code list
		 */
		private function parseVacationList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:TeamVacationVO = new TeamVacationVO();
				vo.PERNR 	= StringUtil.trimFront(rec.PERNR, "0");
				vo.ENAME 	= rec.ENAME;
				vo.AWART	= rec.AWART;
				vo.AWATX	= rec.AWATX;
				vo.BEGDA	= DateUtil.stringToDateTime(rec.BEGDA);
				vo.ENDDA	= DateUtil.stringToDateTime(rec.ENDDA);
				
				retArr.addItem(vo);
			}
			return retArr;	
		}
	}
}